<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/12
 * Time: 1:43 AM
 */

namespace api\models\search;


use api\data\ApiDataProvider;
use api\models\Job;
use api\models\PartTimeJob;
use backend\models\search\PartTimeJobSearch;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Expression;

class PartTimeSearch extends PartTimeJob
{

    public $ad_code;
    public $job_type;
    public $salary;
    public $time; //工作时间


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'job_type',
                    'salary',
                    'time'
                ],
                'integer'
            ],
            [
                [
                    'ad_code',
                ],
                'safe'
            ],
        ];
    }


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PartTimeJob::find()->select("job.*");
        $query->joinWith('jobInfo');
        $query->joinWith('u');
        // add conditions that should always apply here


        $dataProvider = new ApiDataProvider([
            'query'      => $query,
            'sort'       => [
                'defaultOrder' => [
                    'created_at' => SORT_DESC,
//                    'sign_end'   => SORT_DESC
                ]
            ],
            'totalCount' => ''
        ]);

        $this->load($params, 'Search');
        if ( ! $this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        switch ($this->ad_code) {
            case $this->ad_code % 10000 == 0:
                $query->addselect(['code' => new Expression("(`ad_code`div 10000)")]);
                $query->having(['code' => $this->ad_code / 10000]);
                break;
            case $this->ad_code % 100 == 0:
                $query->addselect(['code' => new Expression("(`ad_code`div 100)")]);
                $query->having(['code' => $this->ad_code / 100]);
                break;
            default:
                $query->filterWhere(['code' => $this->ad_code]);
                break;
        }


        // grid filtering conditions
        $query->andFilterWhere(['job_type' => $this->job_type])
              ->andFilterWhere(['>=', 'salary', $this->salary])
              ->andFilterWhere(['>=', 'sign_end', time()])
              ->andFilterWhere([
                  'and',
                  ['<=', 'start_at', $this->time],
                  ['>=', 'end_at', $this->time]
              ]);

        $query->andwhere(['forbidden' => Job::ACCESS, 'type' => PartTimeJob::PARTTIME_TYPE, 'hide' => Job::ACCESS]);

        return $dataProvider;
    }

}